<?php
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

class GroupController extends AdminBaseController{
    public function index(){
        $group = M('group');
        $groupInfo = $group -> select();
        $this -> assign('list',$groupInfo);
        $this -> display();
    }
    public function add(){
        $this -> display();
    }
    public function edit(){
        if(IS_GET){
            $id = I('get.id/d',0);
            if(empty($id)){
                $this -> redirect('Admin/Group/index');
            }
            $group = M('group');
            $groupInfo = $group -> where(['id' => $id]) ->find();
            $this -> assign('groupInfo',$groupInfo);
            $this->display();
        }
    }
    public function groupDel(){
        if(IS_POST){
            $id = I('post.id/d');
            $admin = M('group');
            if(($id != 1) && !empty($id)){
                $map['id'] = $id;
                $admin -> where($map) -> delete();
                $this->ajaxReturn([ 'title' => '删除成功！','msg' => '您已经永久删除了这条信息。','status' => 'success']);
            }else{
                $this->ajaxReturn(['title' => '删除失败！','msg' => '非法操作！','status' => 'error']);
            }
        }else{
            $this->ajaxReturn(['title' => '删除失败！','msg' => '非法操作！','status' => 'error']);
        }
    }
    public function groupChange(){
        if(IS_POST){
            $id = I('post.id/d');
            $admin = M('group');
            if(($id != 1) && !empty($id)){
                $map['id'] = $id;
                $data = $admin -> field('status') -> where($map) -> find();
                if(!empty($data)){
                    if($data['status'] == 1 ){
                        $change['status'] = 0;
                        $admin -> where($map) -> save($change);
                        $this->ajaxReturn(['msg' => '禁用成功！','status' => 1,'state' => 0]);
                    }else{
                        $change['status'] = 1;
                        $admin -> where($map) -> save($change);
                        $this->ajaxReturn(['msg' => '启用成功！','status' => 1,'state' => 1]);
                    }
                }else{
                    $this->ajaxReturn(['msg' =>'非法操作','status' => 0]);
                }
            }else{
                $this->ajaxReturn(['msg' =>'非法操作','status' => 0]);
            }

        }else{
            $this->ajaxReturn(['msg' =>'非法操作','status' => 0]);
        }
    }
    public function save(){
        $id = I('post.id/d',0);
        if(empty($id)){
            if(IS_POST){
                $title = I('post.title/s');
                $status = I('post.status/d');
                if((empty($title))&& (empty($status))){
                    $this->ajaxReturn(['msg' => '发生错误，请重新填写!','status' => '0']);
                }
                $admin = M('group');
                $adminInfo =$admin -> where(['title' => $title])->find();
                if($adminInfo){
                    $this->ajaxReturn(['msg' => '该分组已存在！','status' => '0']);
                }
                $data['title'] =$title;
                $data['status'] =$status;
                $result = $admin ->data($data) -> add();
                if(isset($result)){
                    $this->ajaxReturn(['msg' => '添加成功！','status' => '1']);
                }
                $this->ajaxReturn(['msg' => '添加失败!','status' => '0']);
            }else{
                $this->ajaxReturn(['msg' => '添加失败!','status' => '0']);
            }
        }else{
            if(IS_POST){
                $title = I('post.title/s');
                if(empty($title)){
                    $this->ajaxReturn(['msg' => '发生错误，请重新修改!','status' => '0']);
                }
                $admin = M('group');
                $data['title'] =$title;
                $result = $admin ->where(['id' => $id]) -> save($data);
                if(isset($result)){
                    $this->ajaxReturn(['msg' => '修改成功！','status' => '1']);
                }
                $this->ajaxReturn(['msg' => '修改失败!','status' => '0']);
            }else{
                $this->ajaxReturn(['msg' => '修改失败!','status' => '0']);
            }
        }
    }
    public function rule(){
        if(IS_GET){
            $id = I('get.id/d',0);
            if(empty($id)){
                $this -> error('发生错误！',U('admin/group/index'));
            }
            $rule = M('Group')->field('rules')->where(['id'=>$id])->find();
            $ruleList = M('rule');
            $data = $ruleList -> where('status = 1') -> select();
            $data = make_tree($data);
            $this->assign('rules',explode(',',$rule['rules']));
            $this -> assign('dataList',$data);
            $this -> assign('id',$id);
            $this ->display();
        }
        if(IS_POST){
            $id =I('post.id/d',0);
            $rule_ids = I('post.rule_ids/a',[]);
            if(empty($id)){
                $this -> error('发生错误！',U('admin/group/index'));
            }
            $data['rules'] = implode(',',$rule_ids);
            $isUP = M('group') -> where(['id'=>$id]) -> save($data);
            if($isUP){
                $this->ajaxReturn(['status' => 1 , 'msg' => '更新成功！']);
            }else{
                $this->ajaxReturn(['status' => 0 , 'msg' => '更新失败！']);
            }
        }
    }
}

